package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketIssue;
import com.cskaoyan.bean.po.MarketIssueExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.service.adminservice.IssueService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 楚夏三分
 * @datetime 2022/12/29 15:52
 * @description: TODO:
 */
@Service
public class IssueServiceImpl implements IssueService {
    @Autowired
    MarketIssueMapper issueMapper;

    /**
     * TODO: 查询问题列表
     * @param info 分页信息
     * @param question 问题模糊查询关键字
     * @return
     */
    @Override
    public CommonData<MarketIssue> list(BasePageInfo info, String question) {
        // 开启分页
        PageHelper.startPage(info.getPage(),info.getLimit());
        // 条件查询
        MarketIssueExample example = new MarketIssueExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketIssueExample.Criteria criteria = example.createCriteria();
        // 模糊查询
        if (!StringUtil.isEmpty(question)){
            criteria.andQuestionLike("%" + question + "%");
        }
        // 查询逻辑存在
        criteria.andDeletedEqualTo(false);
        List<MarketIssue> marketIssuesList = issueMapper.selectByExample(example);
        PageInfo<MarketIssue> marketIssuePageInfo = new PageInfo<>(marketIssuesList);
        return CommonData.data(marketIssuePageInfo);
    }

    /**
     * TODO: 新增问题issue
     * @param question 问题
     * @param answer 解答
     * @return
     */
    @Override
    public MarketIssue create(String question, String answer) {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setAnswer(answer);
        marketIssue.setQuestion(question);
        marketIssue.setAddTime(new Date());
        marketIssue.setDeleted(false);
        issueMapper.insert(marketIssue);
        return marketIssue;
    }

    /**
     * TODO: 编辑问题, 更新updateTime
     * @param issue 问题json对象
     * @return
     */
    @Override
    public MarketIssue update(MarketIssue issue) {
        issue.setUpdateTime(new Date());
        issueMapper.updateByPrimaryKey(issue);
        return issue;
    }

    @Override
    public void delete(Integer id) {
        issueMapper.deleteByIdLogically(id);
    }


}
